<template>
  <div class="box">
    <h2>
      秒杀活动页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增活动</el-button
        ></small
      >
    </h2>
    <el-table :data="seck.SecKList" width="100%">
      <el-table-column prop="id" label="活动序号" width="80"> </el-table-column>
      <el-table-column prop="title" label="活动名称" width="100">
      </el-table-column>
      <el-table-column prop="goodsid" label="商品编号" width="80">
      </el-table-column>
      <el-table-column
        prop="first_cateid"
        label="一级分类编号"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="second_cateid"
        label="二级分类编号"
        width="120"
      ></el-table-column>
      <el-table-column prop="begintime" label="活动开始时间">
        <template v-slot="props">
          <el-tag type="success" size="small">{{
            formatTime(props.row.begintime)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="endtime" label="活动结束时间">
        <template v-slot="props">
          <el-tag type="danger" size="small">{{
            formatTime(props.row.endtime)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="活动状态">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1"
            >启用</el-tag
          >
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="primary"
            size="small"
            round
            @click="editSeck(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="delSeck(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗 -->
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, reactive, onMounted } from "vue";
// 导入弹窗组件
import Dialog from "./dialog.vue";
// 导入store
import useSeckillStore from "../../stores/seck";
// 定义
const seck = useSeckillStore();

//
const $router = useRouter();

// 获取数据
onMounted(() => {
  // 调用函数,获取菜单数据
  seck.getSecKListActions();
});
// 时间
const formatTime = (time) => {
  // console.log("time", typeof time); string类型
  const date = new Date(Number(time));
  return `${date.getFullYear()}年${(date.getMonth() + 1)
    .toString()
    .padStart(2, 0)}月${date.getDate().toString().padStart(2, 0)}日 ${date
    .getHours()
    .toString()
    .padStart(2, 0)}时${date.getMinutes().toString().padStart(2, 0)}分${date
    .getSeconds()
    .toString()
    .padStart(2, 0)}秒`;
};

// 弹窗变量
const VDialog = ref(null);
// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};

//删除
function delSeck(Secks) {
  console.log("Secks", Secks);
  ElMessageBox.confirm("确定删除此活动吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      seck.delSeckActions(Secks.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此活动",
      });
    });
}
// 编辑
function editSeck(Secks) {
  // console.log("11111111111111", VDialog.value, Secks.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Secks);
  showDialog();
}
</script>

<style lang="css" scoped></style>
